<?php namespace app\api\controller;

use app\common\controller\AuthAndBind;
use think\Controller;
use think\Db;
use alisms\SendSms;
use esign\EsignHandle;

/**
 * api
 */
class Address extends AuthAndBind{
	
	public function __construct(){
		parent::__construct();
	}
	
	/**
	 * 详情
	 */
	public function details(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['id', 'eq', $in['id']];
				
				$data = Db::table("gd_address") -> where($where) -> find();
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'获取地址详情',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 批量删除
	 */
	public function batchDelete(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['id', 'in', $in['id']];
				$oldData = Db::table("gd_address") -> where($where) -> select();
				
				if( !empty($oldData) ){
					$delRes = Db::table("gd_address") -> where($where) -> delete();
					if( !$delRes ) returnResults(['code'=>'-1', 'msg'=>'操作失败,请联系管理员']);
				}
				
				$outArr['code'] = '1';
				
//				写入操作日志
				$old_data_json = $oldData;
				controller("service/OperLog") -> logs(
					[
						'title'=>'批量删除地址',
						'business_type'=>2,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 列表
	 */
	public function index(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$num = isset($in['num']) ? $in['num'] : 20;
				$start = isset($in['page']) && $in['page'] > 0 ? (($in['page'] - 1) * $num) : 0;
				
				$where[] = ['is_delete', 'eq', 0];
				if( isset($userInfo['auth_group']) && $userInfo['auth_group']['path'] != 'ALL' ) $where[] = ['create_user_id', 'eq', $userInfo['info']['id']];						//非总平台
				if( isset($in['create_date']) ) $where[] = ['create_date', 'between', $in['create_date']];
				if( isset($in['create_user_id']) ) $where[] = ['create_user_id', 'eq', $in['create_user_id']];
				if( isset($in['source']) ) $where[] = ['source', 'eq', $in['source']];
				
				$data = Db::table("gd_address") -> where($where) -> order("id desc") -> limit($start, $num) -> select();
				$count = Db::table("gd_address") -> where($where) -> count();
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				$outArr['totalNums'] = $count;				
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'获取地址列表',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 编辑地址
	 */
	public function edit(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['id', 'eq', $in['id']];
				$oldData = Db::table("gd_address") -> where($where) -> find();
				if( empty($oldData) ) returnResults(['code'=>'-1', 'msg'=>'原数据有误']);
				
				$save = [];
				foreach ($in as $k => $v) {
					if( !in_array($k, ['id', 'user_id']) ){
						$save[$k] = $v;
					}
				}
				
				$save = filterArr($save, array_keys($save), $oldData);
				
				if( !empty($save) ){
					
					Db::startTrans();
					
					if( isset($save['is_default']) && $save['is_default'] == 1 ){
						$oldDataListWhere[] = ['create_user_id', 'eq', $oldData['create_user_id']];
						$oldDataListWhere[] = ['is_default', 'eq', 1];
						$oldDataList = Db::table("gd_address") -> where($oldDataListWhere) -> find();
						if( !empty($oldDataList) ){
							$res = Db::table("gd_address") -> where($oldDataListWhere) -> update(['is_default'=>0]);
							if( !$res ) {
								Db::rollback();
								returnResults2('-1', '去除默认失败,请联系管理员');
							}
						}
					}
					
					$re = Db::table("gd_address") -> where($where) -> update($save);
					if( !$re ){
						Db::rollback();
						returnResults2('-1', '编辑失败,请联系管理员');
					}
					Db::commit();
				}
				$outArr['code'] = '1';
				
//				写入操作日志
				$old_data_json = $oldData;
				controller("service/OperLog") -> logs(
					[
						'title'=>'编辑地址',
						'business_type'=>3,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 新增地址
	 */
	public function add(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$newData = [];
				foreach ($in as $k => $v) {
					if( !in_array($k, ['id', 'user_id']) ){
						$newData[$k] = $v;
					}
					$newData['create_user_id'] = $in['user_id'];
					$newData['create_user_name'] = $userInfo['info']['nickname'];
					$newData['create_user_type'] = $userInfo['info']['type'];
					$newData['create_date'] = date("Y-m-d H:i:s", time());
				}
				
				Db::startTrans();
				
				if( isset($newData['is_default']) && $newData['is_default'] == 1 ){
					$oldDataListWhere[] = ['create_user_id', 'eq', $newData['create_user_id']];
					$oldDataListWhere[] = ['is_default', 'eq', 1];
					$oldDataList = Db::table("gd_address") -> where($oldDataListWhere) -> find();
					if( !empty($oldDataList) ){
						$res = Db::table("gd_address") -> where($oldDataListWhere) -> update(['is_default'=>0]);
						if( !$res ) {
							Db::rollback();
							returnResults2('-1', '去除默认失败,请联系管理员');
						}
					}
				}
				
				$re = Db::table("gd_address") -> insertGetId($newData);
				if( !$re ) {
					Db::rollback();
					returnResults2('-1', '新增失败,请联系管理员');
				}
				
				Db::commit();
				$outArr['code'] = '1';
				$outArr['info'] = $re;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'新增地址',
						'business_type'=>1,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
}

?>